import { Animation, AnimationState, director, Vec2 } from 'cc'
import { GameEvent } from '../../event/gameEvent'
import { StateDefine } from "../StateDefine"
import { ActorState } from "./ActorState"
export class Die extends ActorState {
    onEnter() {
        this.actor.rigidBody.linearVelocity = Vec2.ZERO
        this.animation.play(StateDefine.Die)
        this.animation.once(Animation.EventType.FINISHED, this.onDieEnd, this)
        this.actor.dead = true
    }

    onDieEnd(type: Animation.EventType, state: AnimationState) {
        if (type === Animation.EventType.FINISHED) {
            if (state.name === StateDefine.Die) {
                this.actor.scheduleOnce(() => {
                    this.actor.node.destroy()
                    // TODO 派发死亡事件
                    director.emit(GameEvent.OnDie, this.actor.node)
                })
            }
        }
    }
    canTransit(to: StateDefine): boolean {
        return false
    }
}

